package com.kpx.app.view;

import android.content.Context;
import android.os.Parcelable;
import android.support.annotation.IdRes;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.kpx.app.R;
import com.kpx.app.adapter.MyPagerAdapter;
import com.kpx.app.bean.HelpBean;
import com.kpx.app.wigdet.ViewShowGrowth;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by congdy on 17/2/27.
 */

public class ViewShowField extends LinearLayout {


    private final Context context;
    @BindView(R.id.viewpager)
    ViewPager viewpager;
    @BindView(R.id.group_tab)
    RadioGroup groupTab;
    @BindView(R.id.ll_container)
    LinearLayout llContainer;
    @BindView(R.id.ll_lines)
    LinearLayout llLines;
    private int current = 0;
    private ArrayList<View> views = new ArrayList<>();

    public ViewShowField(Context context) {
        super(context);
        this.context = context;
        View view = View.inflate(context, R.layout.view_show_field, this);
        ButterKnife.bind(view);
        viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                groupTab.check(position);
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
//        groupTab.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
//            @Override
//            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
//                viewpager.setCurrentItem(checkedId);
//            }
//        });
    }

    public void setData(HelpBean bb) {
        List<HelpBean.ChildsBean> glist = bb.getChilds();
        if (glist != null && glist.size() > 0) {
            for (int i = 0; i < glist.size(); i++) {
                final RadioButton b = (RadioButton) View.inflate(context, R.layout.radio_button_show, null);
                b.setId(i);
                b.setChecked(i == 0);
                b.setText(glist.get(i).getName());
                b.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewpager.setCurrentItem(b.getId());
                    }
                });
                groupTab.addView(b);
                RadioGroup.LayoutParams params = (RadioGroup.LayoutParams) b.getLayoutParams();
                params.weight = 1;
                params.height = RadioGroup.LayoutParams.MATCH_PARENT;
                params.width = 0;
                b.setLayoutParams(params);
                ViewShowGrowth viewShowGrowth = new ViewShowGrowth(context, glist.get(i));
                views.add(viewShowGrowth);
                ViewLine line = new ViewLine(context);
                line.setSelected(i==0);
                llLines.addView(line);
                LinearLayout.LayoutParams p = (LayoutParams) line.getLayoutParams();
                p.weight=1;
                p.width = 0;
                line.setLayoutParams(p);
            }
            viewpager.setAdapter(new MyPagerAdapter(views));
            viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                    llLines.getChildAt(current).setSelected(false);
                    llLines.getChildAt(position).setSelected(true);
                    current=position;
                }

                @Override
                public void onPageSelected(int position) {

                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
        }
    }

    /**
     * viewpager的适配器
     *
     * @Description TODO
     * @author chencong
     * @date 2015年1月10日
     * @Copyright: Copyright (c) 2015 Shenzhen Tentinet Technology Co., Ltd. Inc. All rights reserved.
     */
    public class MyPagerAdapter extends PagerAdapter {

        /** 装有内容页面的list */
        public List<View> mListViews;

        public MyPagerAdapter(List<View> mListViews) {
            this.mListViews = mListViews;// 接受list
        }
        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView(mListViews.get(arg1));// 移除不需要的页面
        }

        @Override
        public void finishUpdate(View arg0) {
        }

        @Override
        public int getCount() {
            return mListViews.size();// 页面的数量
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            ((ViewPager) arg0).addView(mListViews.get(arg1), 0);// 添加页面
            return mListViews.get(arg1);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == (arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
        }
    }
}
